package hasalah.demo.client.views;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootPanel;

public class DefaultPanel extends Composite {

	private final DecoratorPanel decoratorPanel;
	private final String title;
	private volatile String whereToGo = null;
	private RootPanel rootPanel;
	
	public DefaultPanel(String title) {

		this.title = title;
		
		decoratorPanel = new DecoratorPanel();
		


	}
	
	public String getWhereToGo() {
		return whereToGo;
	}
	
	public void setWhereToGo(String toGo, RootPanel panel) {
		this.whereToGo = toGo;
		this.rootPanel = panel;
	}

	public void setBody(IsWidget widget) {

		decoratorPanel.setWidget(widget);
		
	}

	public IsWidget getMe() {

		FlexTable flexTable = new FlexTable();
		
		flexTable.addStyleName("cw-FlexTable");
		flexTable.setWidth("600px");
		flexTable.setCellSpacing(5);
		flexTable.setCellPadding(3);
				
		// Adding a title
		flexTable.setWidget(1, 1, new HTML(this.title));
		
		flexTable.getCellFormatter().setAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		// Adding the panel
		flexTable.setWidget(2, 1, this.decoratorPanel);
		
		// Adding the buttons
		FlexTable buttonTable = new FlexTable();
	
		buttonTable.setCellSpacing(5);
		buttonTable.setCellPadding(3);
		buttonTable.setWidth("600px");
		
		buttonTable.addStyleName("cw-FlexTable");
		buttonTable.setCellSpacing(5);
		buttonTable.setCellPadding(3);

		Button nextButton = new Button("Next");
		buttonTable.setWidget(1, 1, nextButton);
		
		buttonTable.setWidget(1 ,3 ,null);
		
		nextButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				
				if(whereToGo != null) {
					
					if(whereToGo.equalsIgnoreCase("A")) {
						
						rootPanel.clear();
						
						DefaultPanel defaultPanel = new DefaultPanel("Acvtivity Configuration ..");
												
						defaultPanel.setBody(new ActivityMainComposite());				
						
						defaultPanel.setWhereToGo("R", rootPanel);

						
						rootPanel.add(defaultPanel.getMe());
						
					} else if(whereToGo.equalsIgnoreCase("R")) {

					

					} else if(whereToGo.equalsIgnoreCase("L")) {


					}
					
				} else {
					
					Window.alert("There where no 'WhereToGo' setten !!!?");
					
				}
				
			}
		});
	
		Button cancelButton = new Button("Cancel");
		buttonTable.setWidget(1, 3, cancelButton);
	
		buttonTable.getCellFormatter().setAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		buttonTable.getCellFormatter().setAlignment(1, 2, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		flexTable.setWidget(3,1, buttonTable);
		
		
		return flexTable;
	}
	
	

}
